/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.opensymphony.workflow.entity;

import java.util.ArrayList;
import java.util.List;

import org.jackysoft.util.ValidationUtils;


/**
 * DOCUMENT ME!
 */


public class JoinDescriptor extends AbstractDescriptor implements Validatable {
    //~ Instance fields ////////////////////////////////////////////////////////

    /**
	 * 
	 */
	private static final long serialVersionUID = 6374557114756627847L;
	protected List conditions = new ArrayList();
    protected ResultDescriptor result;

    //~ Constructors ///////////////////////////////////////////////////////////

    /**
     * 
     */
   public JoinDescriptor() {
    }

   
    /**
     * 
     */
    public JoinDescriptor(org.dom4j.Element element) {
    	super(element);this.init(element);
    }

    //~ Methods ////////////////////////////////////////////////////////////////
   
    public List getConditions() {
        return conditions;
    }

    public void setResult(ResultDescriptor result) {
        this.result = result;
    }

    
    
    public ResultDescriptor getResult() {
        return result;
    }

    public void validate() throws InvalidWorkflowDescriptorException {
        ValidationUtils.validate(conditions);

        if (result == null) {
            throw new InvalidWorkflowDescriptorException("Join has no result");
        }

        result.validate();
    }
 
        
    @Override
    protected void init(org.dom4j.Element join) {
    	try {
    		setId(Integer.parseInt(join.attributeValue("id")));
    	} catch (Exception ex) {
    		throw new IllegalArgumentException("Invalid join id value " + join.attributeValue("id"));
    	}
    	
    	// get conditions
    	super.buildElements(join, "conditions", ConditionsDescriptor.class, this.conditions, this);

    	//<unconditional-result status="Underway" owner="test" step="2"/>
    	org.dom4j.Element resultElement = join.element("unconditional-result");
    	
    	//[KAP] This allows loading a workflow with Joins without unconditional-results
    	if (resultElement != null) {
    		result = new ResultDescriptor(resultElement);
    		result.setParent(this);
    	}
    }
}
